<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Redirect;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\validator;
class UserController extends CommonController{
    public $logo = '';
    public function hy_list($key=""){
        if(empty($key)){
            $users = DB::table('mo_user')->where(['u_status'=>1])->paginate(1);
        }else{
            $users = DB::table('mo_user')->where(['u_status'=>1])->where('u_name', 'like', '%'.$key.'%')->paginate(1);
        }
        return view('admin.user.hy_list',compact('users'));
    }
    public function edit($id){
        if(session('r_id') != 1){
            $res = $this->ifright();
            if($res == "no" || $res == NULL){
                $title = array('content'=>'你暂时没有此权限','url'=>'hy_list');
                return view('admin.alert',['data'=>$title]);
            }else{
                $arr = DB::table('mo_user')->where(['uid'=>$id])->first();
                return view('admin.user.edit',array('arr'=>$arr));
            }
        }else{
            $arr = DB::table('mo_user')->where(['uid'=>$id])->first();
            return view('admin.user.edit',array('arr'=>$arr));
        }
    }
    public function edit_do(Request $request){
        $u_name = $request->input('u_name');
        $u_sex = $request->input('u_sex');
        $u_phone = $request->input('u_phone');
        $uid = $request->input('uid');

        //自动验证
        $this->validate($request , [
            'u_name'	=>'required|max:30',
            'u_phone'=>'required|regex:/^1[35874]\d{9}$/',
        ],
            [
                'u_name.required' => '用户名必须填写',
                'u_name.max'		=> '用户名最多填写30个字符（一个中文占用两个字符）',
                'u_phone.required'=>'手机号不能为空',
                'u_phone.regex'=>'手机号格式不对',
            ]
        );
        $data['uid'] = $uid;
        $data['u_name'] = $u_name;
        $data['u_sex'] = $u_sex;
        $data['u_phone'] = $u_phone;
        if ($request->isMethod('POST')) {
            //显示的属性更多
            $fileCharater = $request->file('slogo');
            if ($request->hasFile('slogo')) {
                $allowed_extensions = ["png", "jpg", "gif","jpeg","JPG","PNG","GIF","JPEG"];
                //获取文件的扩展名
                $ext = $fileCharater->getClientOriginalExtension();
                if(in_array($ext,$allowed_extensions)){
                    //定义文件名
                    $filename = date('Y-m-d-H-i-s').'.'.$ext;
                    $fileCharater->move("admin_style/uploads",$filename);
                    $data['u_portrait'] = $filename;
                    DB::table('mo_user')->where(['uid'=>$data['uid']])->update($data);
                }else{
                    $title = array('content'=>'图片格式不正确','url'=>'user/hy_list');
                    return view('admin.user.alert',['data'=>$title]);
                }
            }
        }
        $res = DB::table('mo_user')->where(['uid'=>$data['uid']])->update($data);
        if($res){
            return Redirect('user/hy_list');
        }else{
            return Redirect('user/hy_list');
        }
    }
    //单删
    public function del($id){
        if(session('r_id') != 1) {
            $res = $this->ifright();
            if ($res == "no" || $res == NULL) {
                $title = array('content' => '你暂时没有此权限', 'url' => 'hy_list');
                return view('admin.alert', ['data' => $title]);
            } else {
                $das["u_status"] = 0;
                DB::table('mo_user')->where(["uid" => $id])->update($das);
                return Redirect('user/hy_list');
            }
        }else{
            $das["u_status"] = 0;
            DB::table('mo_user')->where(["uid" => $id])->update($das);
            return Redirect('user/hy_list');
        }
    }
    //多删
    public function dels($str){
        if(session('r_id') != 1) {
            $res = $this->ifright();
            if ($res == "no" || $res == NULL) {
                $title = array('content' => '你暂时没有此权限', 'url' => 'hy_list');
                return view('admin.alert', ['data' => $title]);
            } else {
                $data["u_status"] = 0;
                $str = explode(",", $str);
                foreach ($str as $v) {
                    DB::table('mo_user')->where(["uid" => $v])->update($data);
                }
                return Redirect('user/hy_list');
            }
        }else{
            $data["u_status"] = 0;
            $str = explode(",", $str);
            foreach ($str as $v) {
                DB::table('mo_user')->where(["uid" => $v])->update($data);
            }
            return Redirect('user/hy_list');
        }
    }

    public function add_do(Request $request){
        $fileCharater = $request->file('slogo');
        if ($request->hasFile('slogo')) {
            //获取文件的扩展名
            $ext = $fileCharater->getClientOriginalExtension();
            $allowed_extensions = ["png", "jpg", "gif","jpeg","JPG","PNG","GIF","JPEG"];
            //获取文件的扩展名
            if(in_array($ext,$allowed_extensions)) {
                //定义文件名
                $filename = date('Y-m-d-H-i-s') . '.' . $ext;
                $fileCharater->move("admin_style/uploads", $filename);
                $this->logo = $filename;
            }else{
                $title = array('content'=>'图片格式不正确','url'=>'hy_list');
                return view('admin.alert',['data'=>$title]);
            }
        }
        //自动验证
        $this->validate($request , [
            'u_name'	=>'required|max:30',
            'password'=>'required|min:4|confirmed',
            'password_confirmation'=>'required',
            'u_phone'=>'required|regex:/^1[3584]\d{9}$/',
            'u_sex'=>'required',
            'u_r_province'=>'required',
            'u_r_city'=>'required',
        ],
            [
                'u_name.required' => '用户名必须填写',
                'u_name.max'		=> '用户名最多填写30个字符（一个中文占用两个字符）',
                'password.required'	=> '密码必须填写',
                'password_confirmation.required'	=> '确认密码必须填写',
                'password.confirmed'=>'密码不一致',
                'password.min'=>'密码至少四位',
                'u_phone.required'=>'手机号不能为空',
                'u_phone.regex'=>'手机号格式不对',
                'u_sex.required'=>'性别不能为空',
                'u_r_province.required'=>'所在省不能为空',
                'u_r_city.required'=>'所在市不能为空',
            ]
        );
        $u_name = $request->input('u_name');
        $u_password = Hash::make($request->input('password'));
        $u_phone = $request->input('u_phone');
        $u_sex = $request->input('u_sex');
        $u_r_province = $request->input('u_r_province');
        $u_r_city = $request->input('u_r_city');
        $u_desc= $request->input('u_desc');
        $u_reg_time = date('Y-m-d H:i:s',time());
            $res = DB::table('mo_user')->insert([
                'u_name'=>$u_name,
                'u_password'=>$u_password,
                'u_phone'=>$u_phone,
                'u_sex'=>$u_sex,
                'u_r_province'=>$u_r_province,
                'u_r_city'=>$u_r_city,
                'u_desc'=>$u_desc,
                'u_reg_time'=>$u_reg_time,
                'u_portrait'=>$this->logo,
            ]);
            if($res){
                return Redirect::to('user/hy_list');
            }else{
                echo "<script>window.history.back()</script>";
            }
    }
    public function add(){
        if(session('r_id') != 1) {
            $res = $this->ifright();
            if ($res == "no" || $res == NULL) {
                $title = array('content' => '你暂时没有此权限', 'url' => 'hy_list');
                return view('admin.alert', ['data' => $title]);
            } else {
                return view('admin.user.add');
            }
        }else{
            return view('admin.user.add');
        }
   }
    public function djgl(){
            return view('admin.user.djgl');
  }
    public function hyjl(){
            return view('admin.user.hyjl_list');
  }
}
?>

